package com.jboa.dao.impl;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.jboa.dao.IBizClaimVoucherDAO;
import com.jboa.vo.BizClaimVoucher;
import com.jboa.vo.PageHelper;
@Repository("voucherDao")
public class BizClaimVoucherDAOImpl extends HibernateDaoSupport implements IBizClaimVoucherDAO {

	/**
	 * 根据员工id获取报销单列表信息
	 */
	public void getBypage(final PageHelper page) {
		getHibernateTemplate().executeFind(
				new HibernateCallback() {
					public Object doInHibernate(Session session)throws HibernateException, SQLException{
						StringBuffer sb=new StringBuffer("from BizClaimVoucher b where 1=1");
						gethql(page, sb);
						Query query=session.createQuery(sb.toString());
						query.setProperties(page.getParams());
						query.setFirstResult(page.startRow());
						query.setMaxResults(page.getPagesize());
						List<BizClaimVoucher> ls=query.list();
						page.setVoucherLs(ls);
						
						sb=new StringBuffer("select count(*) from BizClaimVoucher b where 1=1");
						gethql(page,sb);
						query=session.createQuery(sb.toString());
						query.setProperties(page.getParams());
						Long res=(Long) query.uniqueResult();
						page.setTotalcount(res);
						return null;
					}

					private void gethql(final PageHelper page, StringBuffer sb) {
						//查询条件：状态
						if(page.getParams().get("statu")!=null&&!page.getParams().get("statu").equals("")){
							sb.append(" and b.status=:statu");
						}
						//创建时间
						if(page.getParams().get("starttime")!=null&&!page.getParams().get("starttime").equals("")){
							sb.append(" and to_char(b.createTime,'yyyymmdd')=to_char(:starttime,'yyyymmdd')");
						}
						//结束时间
						if(page.getParams().get("endtime")!=null&&!page.getParams().get("endtime").equals("")){
							sb.append(" and to_char(b.modifyTime,'yyyymmdd')=to_char(:endtime,'yyyymmdd')");
						}
						//普通员工
						if(page.getParams().get("sn")!=null){
							sb.append(" and b.sysEmployeeByCreateSn.sn=:sn");
						}
						//部门
						if(page.getParams().get("dept")!=null){
							sb.append(" and b.sysEmployeeByCreateSn.sysDepartment.name=:dept and b.status not in ('新创建')");
						}
						//待处理人为当前用户
						if(page.getParams().get("dealEmployee")!=null){
							sb.append(" and b.sysEmployeeByNextDealSn.sn=:dealEmployee and b.status not in ('新创建')");
						}
					}
				}
			);
	}
	
	/**
	 * 添加报销单
	 */
	public void save(BizClaimVoucher voucher) {
		getHibernateTemplate().save(voucher);
		
	}
	
	/**
	 * 修改报销单
	 */
	public void update(BizClaimVoucher voucher) {
		getHibernateTemplate().update(voucher);
	}
	
	/**
	 * 根据id获取报销单信息
	 */
	public BizClaimVoucher getById(Long id) {
		BizClaimVoucher voucher=getHibernateTemplate().get(BizClaimVoucher.class, id);
		return voucher;
	}

}
